package db

import (
	"com.jiaguyu/model"

	"com.jiaguyu/logger"
	"fmt"

	"github.com/jmoiron/sqlx"
)

func CreatePostComment(comment *model.Comment) (err error) {
	//多表插入  事务
	tx, err := DB.Beginx()
	if err != nil {
		logger.Error("插入事务失败err%V", err)
		return
	}

	sqlstr := `insert into comment(
				 comment_id,  content, author_id,like_count,comment_count)
			   values(?,?,?,?,?)`

	_, err = tx.Exec(sqlstr, comment.CommentId, comment.Content,
		comment.AuthorId, comment.LikeCount, comment.CommentCount)
	if err != nil {
		//logger.Error("create question failed, question:%#v, err:%v", question, err)
		fmt.Printf("提交问题失败comment，err%v\n", err)
		tx.Rollback()
		return
	}
	sqlstr = `insert into comment_rel(comment_id,parent_id,level,question_id,reply_author_id,reply_comment_id)values(?,?,?,?,?,?)`
	_, err = tx.Exec(sqlstr, comment.CommentId, comment.ParentId,
		1, comment.QuestionId, comment.ReplyAuthorId, comment.ReplyCommentId)
	if err != nil {
		//logger.Error("create question failed, question:%#v, err:%v", question, err)
		fmt.Printf("提交问题失败comment_rel，err%v\n", err)
		tx.Rollback()
		return
	}

	//更新 answer的计数
	sqlstr = `update answer set comment_count=comment_count+1 where answer_id =?`
	_, err = tx.Exec(sqlstr, comment.QuestionId)
	if err != nil {
		//logger.Error("create question failed, question:%#v, err:%v", question, err)
		fmt.Printf("update  计数失败，err%v\n", err)
		tx.Rollback()
		return
	}
	err = tx.Commit()
	if err != nil {
		fmt.Printf("提交问题失败回滚，err%v\n", err)
		tx.Rollback()
		return
	}
	return
}

func CreateReplyComment(comment *model.Comment) (err error) {
	//多表插入  事务
	tx, err := DB.Beginx()
	if err != nil {
		logger.Error("插入事务失败err%V", err)
		return
	}
	//根据replycommentid 查询对应的quthhorid
	var ReplyAuthorId int64
	sqlStr := `select author_id from comment where comment_id=?`

	err = tx.Get(&ReplyAuthorId, sqlStr, comment.ReplyCommentId)
	if err != nil {
		//logger.Error("create question failed, question:%#v, err:%v", question, err)
		fmt.Printf("提交问题失败 ReplyAuthorId，err%v\n", err)
		return
	}
	if ReplyAuthorId == 0 {
		err = fmt.Errorf("查询回复作者id没有")
		return
	}
	comment.ReplyAuthorId = ReplyAuthorId
	sqlstr := `insert into comment(
				 comment_id,  content, author_id)
			   values(?,?,?)`

	_, err = tx.Exec(sqlstr, comment.CommentId, comment.Content,
		comment.AuthorId)
	if err != nil {
		//logger.Error("create question failed, question:%#v, err:%v", question, err)
		fmt.Printf("提交问题插入，err%v\n", err)
		tx.Rollback()
		return
	}
	sqlstr = `insert into comment_rel(comment_id,parent_id,level,question_id,reply_author_id,reply_comment_id)values(?,?,?,?,?,?)`
	_, err = tx.Exec(sqlstr, comment.CommentId, comment.ParentId,
		2, comment.QuestionId, comment.ReplyAuthorId, comment.ReplyCommentId)
	if err != nil {
		//logger.Error("create question failed, question:%#v, err:%v", question, err)
		fmt.Printf("提交问题失败--comment_rel，err%v\n", err)
		tx.Rollback()
		return
	}

	//更新 answer的计数
	sqlstr = `update comment set comment_count=comment_count+1 where comment_id =?`
	_, err = tx.Exec(sqlstr, comment.ParentId)
	if err != nil {
		fmt.Printf("更新计数失败，err%v\n", err)
		tx.Rollback()
		return
	}
	err = tx.Commit()
	if err != nil {
		fmt.Printf("提交问题失败回滚，err%v\n", err)
		tx.Rollback()
		return
	}
	return
}

func GetCommentList(answerId int64, offset, limit, level int64) (commentList []*model.Comment, count int64, err error) {
	//level ==1
	sqlStr := `select comment_id from comment_rel where question_id=? and level=? limit ?,?`
	var commentIdList []int64

	err = DB.Select(&commentIdList, sqlStr, answerId, level, offset, limit)

	if err != nil {
		logger.Error("查询第一级level==1失败%V", err)
		return
	}

	if len(commentIdList) == 0 {
		return
	}
	//拿到idlist   查询全部的数据

	var commentIdTempArr []interface{}
	for _, commentId := range commentIdList {
		commentIdTempArr = append(commentIdTempArr, commentId)
	}
	sqlStr = `select  comment_id,content,author_id,comment_count,create_time from comment where comment_id in (?)`
	queryList, argsList, err := sqlx.In(sqlStr, commentIdTempArr)

	if err != nil {
		logger.Error("sqlx 失败%V", err)
		return
	}
	fmt.Println(queryList, argsList, err)
	err = DB.Select(&commentList, queryList, argsList...)
	if err != nil {
		logger.Error("commentlist失败%V", err)
		return
	}

	sqlStr = `select  count(comment_id)  from comment_rel where question_id=?`
	err = DB.Get(&count, sqlStr, answerId)
	if err != nil {
		logger.Error("count 计算失败", err)
		return
	}
	return
}
func GetReplyCommentList(comment_id int64, offset, limit, level int64) (commentList []*model.Comment, count int64, err error) {
	//level ==1
	sqlStr := `select comment_id from comment_rel where parent_id=? and level=? limit ?,?`
	var replyCommentIdList []int64

	err = DB.Select(&replyCommentIdList, sqlStr, comment_id, level, offset, limit)

	if err != nil {
		logger.Error("查询第2级level==2失败%V", err)
		return
	}

	if len(replyCommentIdList) == 0 {
		return
	}
	//拿到idlist   查询全部的数据

	var commentIdTempArr []interface{}
	for _, replyCommentId := range replyCommentIdList {
		commentIdTempArr = append(commentIdTempArr, replyCommentId)
	}
	sqlStr = `select  comment_id,content,author_id,comment_count,create_time from comment where comment_id in (?)`
	queryList, argsList, err := sqlx.In(sqlStr, commentIdTempArr)

	if err != nil {
		logger.Error("sqlx 失败%V", err)
		return
	}
	fmt.Println(queryList, argsList, err)
	err = DB.Select(&commentList, queryList, argsList...)
	if err != nil {
		logger.Error("replycommentlist失败%V", err)
		return

	}
	sqlStr = `select  count(comment_id)  from comment_rel where parent_id=?`
	err = DB.Get(&count, sqlStr, comment_id)
	if err != nil {
		logger.Error("回复count 计算失败", err)
		return
	}
	return
}

func UpdateCommentCount(comment_id int64) (err error) {
	sqlStr := `update comment set like_count=like_count+1 where comment_id =?`
	_, err = DB.Exec(sqlStr, comment_id)
	if err != nil {
		logger.Error("更新点赞数据失败%v", err)
		return
	}
	return
}
